package me.sauce.folkpocket48

import android.app.Application
import android.content.Context
import android.os.StrictMode
import com.orhanobut.logger.AndroidLogAdapter
import com.orhanobut.logger.DiskLogAdapter
import com.orhanobut.logger.Logger
import com.orhanobut.logger.PrettyFormatStrategy
import com.squareup.leakcanary.LeakCanary
import me.sauce.folkpocket48.data.DataResponse
import java.lang.ref.WeakReference


/**
 * @author sauceWu .
 * @since 2017/12/20 17:54
 */
class App : Application() {
    override fun onCreate() {
        super.onCreate()
        INSTANCE = WeakReference(this)
        setupLeakCanary()
        initLogger()
        DataResponse.get().init(this)
    }

    private fun initLogger() {
        val formatStrategy = PrettyFormatStrategy.newBuilder().tag("Pocket48").build()
        Logger.addLogAdapter(object : AndroidLogAdapter(formatStrategy) {
            override fun isLoggable(priority: Int, tag: String?): Boolean {
                return BuildConfig.DEBUG
            }
        })
        Logger.addLogAdapter(DiskLogAdapter(formatStrategy))

    }

    private fun setupLeakCanary() {
        if (LeakCanary.isInAnalyzerProcess(this)) {
            return
        }
        enabledStrictMode()
        LeakCanary.install(this)

    }

    private fun enabledStrictMode() {
        StrictMode.setThreadPolicy(StrictMode.ThreadPolicy.Builder()
                .detectAll()
                .penaltyLog()
                .penaltyDialog()
                .build())

    }

    companion object {
        lateinit var INSTANCE: WeakReference<Context>
    }
}